﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GestIT;
using System.Collections.ObjectModel;
namespace BodyGestIT
{
    public abstract class GestureComponent : UserControl
    {
        internal abstract TmpExp InstantiateExp();

        protected GestureComponent()
        {
            this.Visibility = Visibility.Collapsed;
        }

        public bool Iterative { get; set; }

        public static readonly DependencyProperty CompletedProperty =
        DependencyProperty.RegisterAttached(
            "CompletedInternal", 
            typeof(Completed),
            typeof(Handler));

        public static Completed GetCompleted(Control item)
        {
            var collection = (Completed)item.GetValue(CompletedProperty);
            if (collection == null)
            {
                collection = new Completed();
                item.SetValue(CompletedProperty, collection);
            }
            return collection;
        }

        public static readonly DependencyProperty PerformingProperty =
       DependencyProperty.RegisterAttached(
           "PerformingInternal", 
           typeof(Performing),
           typeof(Handler));

        public static Performing GetPerforming(Control item)
        {
            var collection = (Performing)item.GetValue(PerformingProperty);
            if (collection == null)
            {
                collection = new Performing();
                item.SetValue(PerformingProperty, collection);
            }
            return collection;
        }

        public static readonly DependencyProperty ErrorProperty =
        DependencyProperty.RegisterAttached(
            "ErrorInternal", 
            typeof(Error),
            typeof(Handler));

        public static Error GetError(Control item)
        {
            var collection = (Error)item.GetValue(ErrorProperty);
            if (collection == null)
            {
                collection = new Error();
                item.SetValue(ErrorProperty, collection);
            }
            return collection;
        }

        protected void AttachEvents(TmpExp exp)
        {
            Completed completed = GestureComposite.GetCompleted(this);
            Error error = GestureComposite.GetError(this);
            Performing performing = GestureComposite.GetPerforming(this);

            foreach (Handler handler in completed)
            {
                if (handler.method != null)
                {
                    exp.Completed += handler.method;
                }
            }

            foreach (Handler handler in error)
            {
                if (handler.method != null)
                {
                    exp.Error += handler.method;
                }
            }

            foreach (Handler handler in performing)
            {
                if (handler.method != null)
                {
                    exp.Performing += handler.method;
                }
            }
        }

    }

    public class Completed : Collection<Handler>
    {
        public override string ToString()
        {
            return "";
        }
    }

    public class Performing : Collection<Handler>
    {
        public override string ToString()
        {
            return "";
        }
    }

    public class Error : Collection<Handler>
    {
        public override string ToString()
        {
            return "";
        }
    }

    public class Handler
    {
        public StateChangedHandler method { get; set; }
    }
}
